home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13037 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: netnews.fast.net!news
  2. From: 73541.1332@compuserve.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ problem with constructor.
  5. Date: 23 Mar 1996 02:41:20 GMT
  6. Organization: via FASTNET(tm) PA/NJ/DE Internet Services
  7. Message-ID: <4ivocg$e9d@nn2.fast.net>
  8. References: <DoGGGp.Muy@latcs1.lat.oz.au>
  9. Reply-To: 73541.1332@compuserve.com
  10. NNTP-Posting-Host: power.gpu.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. Gregary,  try new'g some storage for street and city before strcpy
  14.  
  15.  
  16.  
  17.  
  18. In <DoGGGp.Muy@latcs1.lat.oz.au>, boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles) writes:
  19. >See astericks
  20. >
  21. >// main.cpp
  22. >
  23. >#include <iostream.h>
  24. >#include "defines.h"
  25. >#include "address.h"
  26. >
  27. >int main()
  28. >{
  29. >     Address Address1(56,"Derby Drive","Epping",3165);
  30. >     Address Address2(22,"Claremont Street","Fawkner",3060);
  31. >
  32. >     /********************************************************
  33. >      After the above two calls Address1 and Address2 contain
  34. >      all the appropriate data however after the next call all
  35. >      3 contain 0 or null strings. WHY?
  36. >      ********************************************************/
  37. >
  38. >     Address Address3(Address1);
  39. >     return(0);
  40. >}
  41. >
  42. >
  43. >
  44. >// address.cpp
  45. >
  46. >#include "address.h"
  47. >#include <string.h>
  48. >
  49. >// Constructors
  50. >Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  51. >{
  52. >     Number=ANumber;
  53. >     strcpy(Street,AStreet);
  54. >     strcpy(City,ACity);
  55. >     Zip=AZip;
  56. >}
  57. >
  58. >Address::Address()
  59. >{
  60. >     Number=0;
  61. >     strcpy(Street,"");
  62. >     strcpy(City,"");
  63. >     Zip=0;
  64. >}
  65. >
  66. >// Copy constructor
  67. >Address::Address(Address& AnAddress)
  68. >{
  69. >     Number=AnAddress.Number;
  70. >     strcpy(Street,AnAddress.Street);
  71. >     strcpy(City,AnAddress.City);
  72. >     Zip=AnAddress.Zip;
  73. >}
  74. >
  75. >// Deconstructor
  76. >Address::~Address()
  77. >{
  78. >     Number=0;
  79. >     strcpy(Street,"");
  80. >     strcpy(City,"");
  81. >     Zip=0;
  82. >}
  83. >
  84. >
  85. >// address.h
  86. >
  87. >#ifndef __ADDRESS_H
  88. >#define __ADDRESS_H
  89. >#define STR_STREET " street"
  90. >
  91. >#include "defines.h"
  92. >
  93. >class Address
  94. >{
  95. >     private : int Number;
  96. >           char *Street;
  97. >           char *City;
  98. >           int Zip;
  99. >
  100. >     public : // Constructors
  101. >          Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  102. >          Address();
  103. >          // Copy constructor
  104. >          Address(Address& AnAddress);
  105. >          // Deconstructor
  106. >          ~Address();
  107. >};
  108. >
  109. >#endif
  110.  
  111.